<template>
  <div class="header">
    <div class="nav-topbar">
      <div class="container">
        <div class="topbar-menu">
          <a href="/index">商城</a> 
        </div>
        <div class="topbar-user">          
          <a href="javascript:;" v-if="!isLogin" @click="login">登录</a>
          <a href="javascript:;" v-if="isLogin" @click="exit">退出</a>
          <a v-if="isLogin" @click="myOrder">我的订单</a>
          <a v-if="isLogin" @click="myAddress">我的地址</a>
          <a href="javascript:;" class="my-cart" @click="goToCart"><span class="icon-cart"></span>购物车</a>
        </div>
      </div>
    </div>
    <div class="nav-header">
      <div class="container">
        <div class="header-logo">
          <a href="/index"></a>
        </div>
        <div class="header-search">
          <div class="wrapper">
            <input type="text"  v-model="keywords" name="keywords" placeholder="请输入产品关键字">
            <a href="javascript:;" @click="doSearch(keywords)"></a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getLocal } from '@/util'
import {logout} from './../service/user'

  export default{
    name:'nav-header',
    data(){
      return {
        phoneList: [],
        keywords:'',
        isLogin: false,
      }
    },
    computed: {
      cartCount(){ 
        return this.$store.state.cartCount;
      }, 
    },
    filters:{
      currency(val){
        if(!val)return '0.00';
        return '￥' + val.toFixed(2) + '元';
      }
    },
    mounted() {
      const token = getLocal('token')
      if (token) {
        this.isLogin = true
      }
      this.keywords=this.$route.params.keywords;
    },
    methods:{
      login(){
        this.$router.push('/login');
      },
      exit(){
        logout();
        this.isLogin = false;
      },
      doSearch(keywords){
        this.$router.push({
          name:'searchResult',
          params:{
            keywords:keywords
          }
          }).catch(err => { window.console.log(err)});
      },
      goToCart(){
        this.$router.push('/cart');
      },
      myOrder() {
        this.$router.push('/order/list');
      },
      myAddress() {
        this.$router.push('/address');
      },
    }
  }
</script>
<style>
  .header{
    .nav-topbar {
      height: 39px;
      line-height: 39px;
      background-color: #333333;
      color:#B0B0B0;
      .container {
        display:flex;
        justify-content: space-between;
        align-items: center;
        a {
          display:inline-block;
          color:#B0B0B0;
          margin-left: 20px;
          font-size: 16px;
        }
        .my-cart{
          width:110px;
          background-color:#1baeae;
          text-align:center;
          color:#ffffff;
          margin-right:0;
          .icon-cart {
            display:inline-block;
            width: 16px;
            height: 12px;
            background:url('/imgs/icon-cart-checked.png') no-repeat center;
            background-size: contain;
            margin-right:4px;
          }
        }
      }
    }
    .nav-header{
      .container{
        position:relative;
        height:112px;
        display:flex;
        justify-content: space-between;
        align-items: center;
        .header-menu {
          display:inline-block;
          width:643px;
          padding-left:209px;
          .item-menu{
            display:inline-block;
            color:#333333;
            font-weight:bold;
            font-size:16px;
            line-height:112px;
            margin-right:20px;
            span {
              cursor:pointer;
            }
            &:hover{
              color: #FF6600;
              .children {
                height: 220px;
                opacity: 1;
              }
            }
            .children{
              position:absolute;
              top:112px;
              left:0;
              width:1226px;
              height:0;
              opacity:0;
              overflow:hidden;
              border-top:1px solid #E5E5E5;
              box-shadow:0px 7px 6px 0px rgba(0, 0, 0, 0.11);
              z-index: 10;
              transition:all .5s;
              background-color: #ffffff;
              .product {
                position:relative;
                float:left;
                width:16.6%;
                height:220px;
                font-size:12px;
                line-height:12px;
                text-align: center;
                a{
                  display:inline-block;
                }
                img{
                  width: auto;
                  height: 111px;
                  margin-top: 26px;
                }
                .pro-img{
                  height: 137px;
                }
                .pro-name{
                  font-weight: bold;
                  margin-top: 19px;
                  margin-bottom: 8px;
                  color: #333333;
                }
                .pro-price{
                  color: #FF6600;
                }
                &:before{
                  content: ' ';
                  position: absolute;
                  top: 28px;
                  right: 0;
                  border-left: 1px solid #d7d7d7;
                  height: 100px;
                  width: 1px;
                }
                &:last-child:before{
                  display:none;
                }
              }
            }
          }
        }
        .header-search {
          width:319px;
          .wrapper {
            height:50px;
            border:1px solid #E0E0E0;
            display: flex;
            align-items:center;
            input {
              border:none;
              box-sizing: border-box;
              border-right:1px solid #E0E0E0;
              width:264px;
              height:50px;
              padding-left:14px;
            }
            a {
              display:inline-block;
              width: 18px;
              height: 18px;
              background:url('/imgs/icon-search.png') no-repeat center;
              background-size: contain;
              margin-left:17px;
            }
          }
        }
      }
    }
  }
</style>